package com.macro.mall.ums.service.register.impl;

import cn.hutool.core.util.StrUtil;
import com.macro.mall.ums.entity.WxMemberEntity;
import com.macro.mall.ums.enums.UserLevelEnum;
import com.macro.mall.ums.service.account.MemberService;
import com.macro.mall.ums.service.account.WxMemberService;
import com.macro.mall.ums.service.account.bo.InsertMemberInput;
import com.macro.mall.ums.service.account.bo.InsertWxMemberInput;
import com.macro.mall.ums.service.register.RegisterService;
import com.macro.mall.ums.service.register.bo.RegisterByWxMpInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * class
 *
 * @author xuyanjun
 * @date 2019-12-09
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private WxMemberService wxMemberService;

    @Autowired
    private MemberService memberService;

    /**
     * 根据微信公众号注册
     *
     * @param input input
     * @return 注册的用户id
     */
    @Override
    public long registerByWxMp(RegisterByWxMpInput input) {
        WxMemberEntity wxUserEntity = wxMemberService.getByOpenId(input.getOpenId());
        Assert.state(wxUserEntity == null, "用户已经存在，不能再次注册。openId:" + input.getOpenId());

        long userId;

        /*
        1.先检查是否存在unionId相同的用户，如果有，则直接使用此unionId的用户id
        2.如果没有则创建新用户
         */
        WxMemberEntity existUnionIdWxUser = null;

        if (StrUtil.isNotBlank(input.getUnionId())) {
            existUnionIdWxUser = wxMemberService.getByUnionId(input.getUnionId());
        }

        if (existUnionIdWxUser != null) {
            userId = existUnionIdWxUser.getMemberId();
        } else {
            String invitationCode = memberService.generateInvitationCode();

            InsertMemberInput insertUserInput = new InsertMemberInput();
            insertUserInput
                    .setUsername(input.getOpenId())
                    .setIcon(input.getAvatarUrl())
                    .setSourceType(input.getUserSourceTypeEnum())
                    .setNickname(input.getNickName())
                    .setGender(input.getGender())
                    .setLevel(UserLevelEnum.FANS.getValue())
                    .setBefId(input.getBefId())
                    .setInvitationCode(invitationCode);

            userId = memberService.insert(insertUserInput);
        }

        InsertWxMemberInput insertWxUserInput = new InsertWxMemberInput();
        insertWxUserInput
                .setMemberId(userId)
                .setCreateIp(input.getRegisterIp())
                .setWxHeadImgUrl(input.getAvatarUrl())
                .setWxNickname(input.getNickName())
                .setWxUnionId(input.getUnionId())
                .setWxOpenId(input.getOpenId())
                .setWxProvince(input.getProvince())
                .setWxCity(input.getCity())
                .setWxCountry(input.getCountry())
                .setSourceType(input.getUserSourceTypeEnum());

        wxMemberService.insert(insertWxUserInput);
        return userId;
    }
}
